"""Show all matches for a list of patterns.
"""

import re


def test_patterns(text: str, patterns: list):
    """
    Given source text and a list of patterns, look for
    matches for each pattern within the text and print
    them to stdout.
    :param text: 要匹配的字符串
    :param patterns: 正则表达式列表
    :return:无
    """

    # Look for each pattern in the text and print the results
    # 每个正则表达式列表元素是一个元组类型，第一个元素为正则表达式，第二个元素为正则表达式的解释
    for pattern, desc in patterns:
        print("'{}' ({})\n".format(pattern, desc))
        print("  '{}'".format(text))
        for match in re.finditer(pattern, text):
            s = match.start()
            e = match.end()
            substr = text[s:e]
            n_backslashes = text[:s].count('\\')
            prefix = '.' * (s + n_backslashes)
            print("  {}'{}'".format(prefix, substr))
        print()
    return


if __name__ == '__main__':
    test_patterns('abbaaabbbbaaaaa',
                  [('ab', "'a' followed by 'b'")]
                  )
